# -*- coding: utf-8 -*-
"""
版权所有：J哥
微信号：wws_0904
author : Laity
Description: 
"""
import sqlite3
import jieba  # 分词
from matplotlib import pyplot as plt  # 绘制
from wordcloud import WordCloud  # 词云
from PIL import Image  # 图片处理
import numpy as np  # 矩阵运算

conn = sqlite3.connect("movie.db")
cur = conn.cursor()
sql = 'SELECT instroduction from movie250'
data = cur.execute(sql)
text = ""
for i in data:
    text = text + i[0]
    # print(type(i))  # <class 'tuple'>
    # print(i[0])
# print(text)

cur.close()
conn.close()

cut = jieba.cut(text)  # 分词
string = ' '.join(cut)
print(string)

# 开始绘图
img = Image.open(r'static/assets/images/音乐.png')
img_array = np.array(img)  # 将图片转换为数组
wc = WordCloud(
    background_color='black',  # 背景颜色
    mask=img_array,
    font_path='msyh.ttc',  # C:\Windows\Fonts  选中文字库
)
wc.generate(string)

fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off')
plt.show()
wc.to_file('output.png')
